package com.atmAnli;

import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;

/**
 * @ClassName ATMSystem
 * @Description ATM案例 ArrayList实现
 * @Author 2214361854
 * @Date 2022/2/17 11:08
 * @Version 1.0
 */
public class ATMSystem {
    public static void main(String[] args) {
        //用于存储全部账户信息
        ArrayList<Account> accounts = new ArrayList<>();
        showMain(accounts);
    }

    /**
     * 系统未登录界面展示
     * @param accounts
     */
    public static void showMain(ArrayList<Account> accounts){
        Scanner sc = new Scanner(System.in);
        while (true) {
            System.out.println("=========欢迎进入ATM系统=========");
            System.out.println("请您输入您想做的操作：");
            System.out.println("1、登录");
            System.out.println("2、开户");
            System.out.print("您可以输入命令了：");
            int command = sc.nextInt();
            switch (command){
                case 1:
                    //登录
                    login(accounts,sc);
                    break;
                case 2:
                    //开户
                    register(accounts,sc);
                    break;
                default:
                    System.out.println("您输入的命令不被支持！");
            }
        }
    }

    /**
     * 用户登录后的操作界面
     * @param accounts
     * @param sc
     */
    public static void loginMain(Account account,Scanner sc,ArrayList<Account> accounts){
        while (true) {
            System.out.println("=========欢迎："+account.getUserName()+" ATM操作界面=========");
            System.out.println("请您输入您想做的操作：");
            System.out.println("1、查询账户");
            System.out.println("2、存款");
            System.out.println("3、取款");
            System.out.println("4、转账");
            System.out.println("5、修改密码");
            System.out.println("6、退出");
            System.out.println("7、注销当前账户");
            System.out.print("您可以输入命令了：");
            int command = sc.nextInt();
            switch (command){
                case 1:
                    //查询账号
                    showAccount(account);
                    break;
                case 2:
                    //存款
                    depositMoney(account,sc);
                    break;
                case 3:
                    //取款
                     withdrawMoney(account,sc);
                    break;
                case 4:
                    //转账
                    transferAccounts(accounts,account,sc);
                    break;
                case 5:
                    //修改密码 修改完成后重新登录账户
                     changePassword(account,sc);
                     return;
                case 6:
                    //退出 loginMain 与 login 都要退出return
                    System.out.println("退出登录，欢迎下次光临!");
                    return;
                case 7:
                    //注销当前账户
                    cancelAccount(accounts,account,sc);
                    return;
                default:
                    System.out.println("您输入的命令不被支持！");
            }
        }
    }

    /**
     * 跟据当前账号，给其他账号转账
     * @param account
     * @param sc
     * @param accounts
     */
    private static void transferAccounts(ArrayList<Account> accounts,Account account, Scanner sc) {
        //判断系统账号是否存在两个账户及以上
        if (accounts.size()<2) {
            System.out.println("对不起，系统中无其他账号，您不可以转账！");
            return;
        }
        //判断当前账户金额是否大于0
        if (account.getMoney()<=0) {
            System.out.println("对不起，您的账户为空！");
            return;
        }
        //转账
        while (true) {
            System.out.println("请您输入对方账户的卡号：");
            String tCardID = sc.next();
            Account tacc = getAccountByCardId(tCardID, accounts);
            //判断账户卡号是否存在
            if (tacc != null) {
                //判断不为自己的卡号
                if (tacc.getCardID().equals(account.getCardID())) {
                    System.out.println("您不可以为自己转账");
                }else {
                    //认证信息录入
                    String userName = "*"+tacc.getUserName().substring(1);
                    System.out.println("请您确认："+userName+"的姓氏");
                    String preName = sc.next();
                    //确认认证信息
                    if (preName.equals(tacc.getUserName().charAt(0))) {
                        System.out.println("请您输入转账的金额：");
                        double money = sc.nextDouble();
                        if (money>account.getMoney()) {
                            System.out.println("您转账的金额，您最多可以转："+account.getMoney());
                        }else{
                            account.setMoney(account.getMoney()-money);
                            tacc.setMoney(tacc.getMoney()+money);
                            System.out.println("成功转账:"+tacc.getUserName()+",转入金额："+money);
                            showAccount(account);
                            return;
                        }
                    }else{
                        System.out.println("认证信息有误！");
                    }
                }
            }else {
                System.out.println("您输入的卡号不存在！");
            }
        }

    }

    /**
     * 注销当前账号
     * @param accounts
     * @param account
     */
    private static void cancelAccount(ArrayList<Account> accounts, Account account, Scanner sc) {
        System.out.println("=========注销账号=========");
        String confirm = "注销账号";
        System.out.println("请您输入"+"\""+confirm+"\"");
        String okconfirm = sc.next();
        if (confirm.equals(okconfirm)) {
            accounts.remove(account);
            System.out.println("已成功注销账户");
        }else{
            System.out.println("您的输入有误，取消注销账户");
        }
    }

    /**
     * 根据当前账号，修改密码功能
     * @param account
     * @param sc
     */
    private static void changePassword(Account account, Scanner sc) {
        System.out.println("=========修改密码=========");
        while (true) {
            System.out.println("请您输入正确的密码：");
            String okPassword = sc.next();
            if (okPassword.equals(account.getPassWord())) {
                System.out.println("请您输入新密码：");
                String newPassword = sc.next();
                System.out.println("请您确认密码：");
                String oknewPassword = sc.next();
                if (newPassword.equals(oknewPassword)&&newPassword!=null) {
                    account.setPassWord(newPassword);
                    return;
                }else {
                    System.out.println("您两次输入的密码不一致或为空！");
                }
            }else{
                System.out.println("当前输入密码不正确~~~");
            }
        }
    }

    /**
     * 根据当前账号，取款功能
     * @param account
     * @param sc
     */
    private static void withdrawMoney(Account account, Scanner sc) {
        double money = account.getMoney();
        double quotaMoney = account.getQuotaMoney();
        if (money>=100) {
            while (true) {
                System.out.println("=========取款功能=========");
                System.out.println("请您输入取款的金额：");
                double withdrawMoney = sc.nextDouble();
                if (withdrawMoney==money||withdrawMoney<money) {
                    if (withdrawMoney<quotaMoney) {
                        account.setMoney(money-withdrawMoney);
                        showAccount(account);
                        return;
                    }else {
                        System.out.println("取款超过单次限额："+account.getQuotaMoney());
                    }
                }else {
                    System.out.println("您的余额不足："+account.getMoney());
                    return;
                }
            }
        }else {
            System.out.println("您的余额不满100");
        }
    }

    /**
     * 根据当前账号，存款功能
     * @param account
     * @param sc
     */
    private static void depositMoney(Account account,Scanner sc) {
        System.out.println("=========存款功能=========");
        System.out.println("请您输入存款的金额：");
        double depositMoney = sc.nextDouble();
        account.setMoney(account.getMoney()+depositMoney);
        showAccount(account);
    }

    /**
     * 查询账号功能
     * @param account
     */
    private static void showAccount(Account account) {
        System.out.println("=========查询"+account.getUserName()+"用户详情=========");
        System.out.println("您的账号信息如下：");
        System.out.println("卡号："+account.getCardID());
        System.out.println("名称："+account.getUserName());
        System.out.println("余额："+account.getMoney());
        System.out.println("单次取现额度："+account.getQuotaMoney());
    }

    /**
     * 完成用户登录
     * @param accounts
     * @param sc
     */
    private static void login(ArrayList<Account> accounts,Scanner sc) {
        if (accounts.size()==0) {
            System.out.println("当前系统没有账号，请您先注册账号！");
            return;
        }
        //用户录入卡号，密码登录
        while (true) {
            System.out.println("请您输入登录的卡号：");
            String cardID = sc.next();
            Account accountByCardId = getAccountByCardId(cardID, accounts);
            if (accountByCardId!=null) {
                System.out.println("请您输入登录的密码:");
                String password = sc.next();
                while (true) {
                    if (accountByCardId.getPassWord().equals(password)) {
                        //登录成功，展示系统登录后的操作界面
                        loginMain(accountByCardId,sc,accounts);
                        //继续结束登录
                        return;
                    }else{
                        System.out.println("登录密码错误！:");
                    }
                }
            }else {
                System.out.println("卡号不存在！");
            }
        }
    }

    /**
     * 用户开户功能
     * @param accounts  账号集合对象
     * @return
     */
    private static void register(ArrayList<Account> accounts,Scanner sc){
        System.out.println("=========用户开户功能=========");
        System.out.println("请您输入开户名称:");
        String name = sc.next();
        String password = "";
        while (true) {
            System.out.println("请您输入开户密码:");
            password = sc.next();
            System.out.println("请您输入确认密码:");
            String okPassword = sc.next();
            //判断两次密码是否一致
            if (password != null && password.equals(okPassword)) {
                break;
            }else {
                System.out.println("两次密码必须一致~~~");
            }
        }
        System.out.println("请您输入单次取款限额:");
        double quotaMoney = sc.nextDouble();
        //生成账号卡号，8位，且不重复
        String cardID = createCardID(accounts);
        //封装一个账户对象
        Account account = new Account(cardID,name,password,quotaMoney);
        accounts.add(account);
        System.out.println("恭喜您，开户成功，您的卡号是："+account.getCardID()+" 请您妥善保管");
    }

    /**
     * 账号8位卡号生成
     * @param accounts
     * @return
     */
    public static String createCardID(ArrayList<Account> accounts){
        while (true) {
            //生成8位卡号
            String cardID = "";
            Random random = new Random();
            for (int i = 0; i < 8; i++) {
                cardID+=random.nextInt(10);
            }
            //查询卡号是否重复
            Account accountByCardId = getAccountByCardId(cardID, accounts);
            if (accountByCardId==null) {
                return cardID;
            }
        }
    }

    /**
     * 卡号查询
     * @param cardID
     * @param accounts
     */
    private static Account getAccountByCardId(String cardID,ArrayList<Account> accounts) {
        for (int i = 0; i < accounts.size(); i++) {
            Account account = accounts.get(i);
            if (account.getCardID().equals(cardID)) {
                return account;
            }
        }
        return null;
    }
}
